package com.shxy.service.impl;

import com.shxy.service.IFtpService;
import com.shxy.util.FileNameUtil;
import com.shxy.util.FtpProperties;
import com.shxy.util.FtpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FtpService implements IFtpService {
    @Autowired
    FtpProperties ftpProperties;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");

    @Override
    public String doUpload(MultipartFile multipartFile, int userId){
        String reFileName = "";
        try {
            //老文件名
            String oldFileName = multipartFile.getOriginalFilename();
            //根据id调用工具类生成新文件名
            String newFileName = FileNameUtil.getFileName(userId);
            //截取老文件名的后缀
            String substring = oldFileName.substring(oldFileName.lastIndexOf("."));
            //将后缀放在新文件名的后面
            newFileName = newFileName + substring;
            //生成路径
            String filePath = simpleDateFormat.format(new Date());
            //上传
            Boolean resultBoolean = FtpUtil.uploadFile(ftpProperties.getHost(), Integer.parseInt(ftpProperties.getPort()), ftpProperties.getUsername(), ftpProperties.getPassword(), ftpProperties.getBasePath(), filePath, newFileName, multipartFile.getInputStream());
            //判断是否上传成功
            if (resultBoolean) {
                //上传成功
                reFileName = String.format("%s/%s/%s",ftpProperties.getHttpPath(), filePath, newFileName);
                System.out.println(reFileName);
            }
            else {
                System.out.println("上传失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reFileName;
    }

    @Override
    public List<String> doUpload(MultipartFile[] multipartFiles) {
        List<String> fileNames = new ArrayList<>();
        //根据id调用工具类生成新文件名
        String newFileName = FileNameUtil.getFileName(0);

        //对文件数组进行遍历
        for (MultipartFile multipartFile : multipartFiles) {
            //老文件名
            String oldFileName = multipartFile.getOriginalFilename();
            //截取老文件名的后缀
            String substring = oldFileName.substring(oldFileName.lastIndexOf("."));
            //将后缀放在新文件名的后面
            newFileName = newFileName + substring;
            //生成路径
            String filePath = simpleDateFormat.format(new Date());
            //上传
            try {
                Boolean resultBoolean = FtpUtil.uploadFile(ftpProperties.getHost(), Integer.parseInt(ftpProperties.getPort()), ftpProperties.getUsername(), ftpProperties.getPassword(), ftpProperties.getBasePath(), filePath, newFileName, multipartFile.getInputStream());
                //判断是否上传成功
                if (resultBoolean) {
                    //上传成功
                    String head = ftpProperties.getHttpPath() + File.separator + filePath + File.separator + newFileName;
                    fileNames.add(head);
                } else {
                    //如果没有上传成功
                    //只要有一个没有上传成功，直接结束上传，不再继续
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return fileNames;
    }
}
